import type { IMenu } from './entity';
import request from '@/api/request'
import { arr2tree } from '@/utils';

export function getMenus() {

  return request({
    url: `/admin/system/menu/list`,
    method: 'get',
    loading: true,
  }).then((deptList) => {
    const list = arr2tree<IMenu>(deptList, { id: 'id' })
    return { list }
  })
}

export function getMenuSelect(treeMode = true) {
  return request({
    url: '/sys/menu/select',
    method: 'get'
  }).then((menuList) => {
    return treeMode ? arr2tree<IMenu>(menuList, { id: 'menuId', rootId: -1 }) : menuList
  })
}

export function getMenuById(id: number) {
  return request({
    url: `/admin/system/menu/info/${id}`,
    method: 'get'
  })
}

export function deleteMenuById(id: number) {
  return request({
    url: `/admin/system/menu/delete/${id}`,
    method: 'post',

  })
}

export function saveMenu(data: IMenu) {
  return request({
    url: `/admin/system/menu/${!data.id ? 'add' : 'update'}`,
    method: 'post',
    data,
    loading: true
  })
}
export function menuUpdateShowStatus(params: any) {
  return request({
    url: `/admin/system/menu/updateShowStatus`,
    method: 'post',
    params,
    loading: true
  })
}